package com.vichat.common.rudp.thread;

import com.vichat.common.rudp.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

public class RudpBusiThread implements Runnable {
    private static transient Logger logger = LoggerFactory.getLogger(RudpBusiThread.class);
    private RudpTransData transData;
    private RudpBusiSrv srvBusi;


    public RudpBusiThread(RudpBusiSrv insrv, RudpTransData indata) {
        transData = indata;
        srvBusi = insrv;
    }

    public void run() {
        long startIme = System.currentTimeMillis();
        RudpMonitorUtil.startBusiThread();
        if (RudpMonitorUtil.monitorFlag) {
            //放入trans监控
            RudpMonitorUtil.callTrans(transData.getStringTrans(), startIme);
        }

        HashMap<Integer, String> reqObj = null;
        HashMap<Integer, String> respObj = null;
        try {
            ParseData busiData = RudpDataUtil.assembelBusiData(transData);
            logger.debug(transData.transId + "业务数据组装完成:" + busiData.toString());
            try {
                reqObj = ProtocolUtil.decodeMap(busiData);
                logger.debug("!!!!!!!!业务数据转换成功" + reqObj.toString());
            } catch (Exception e) {
                logger.error("业务数据转换错误:" + busiData.toString(), e);
                respObj = new HashMap<Integer, String>();
                respObj.put(RudpParamDefine.cmd_param_code, SendResultEnum.BILL_ERR_INVALID_MESSAGE.value());
            }


            if (startIme - transData.createTime >= 1000) {
                try {
                    if (RudpMonitorUtil.monitorFlag) {
                        //放入trans监控
                        RudpMonitorUtil.expireTrans(transData.getStringTrans(), System.currentTimeMillis());

                    }
                    //处理超时。不调用实际计费服务
                    respObj = reqObj;
                    respObj.put(RudpParamDefine.cmd_param_code, SendResultEnum.BILL_ERR_GIFT_SRV_BUSY.value());

                } catch (Exception e) {
                    logger.error(transData.transId + "处理超时错误", e);
                }
            } else {
                //调用计费相关服务
                try {
                    if (reqObj != null && reqObj.containsKey(RudpParamDefine.cmd_param_cmd_type)) {

                    	respObj = srvBusi.doBusi(reqObj);

                        if (RudpMonitorUtil.monitorFlag) {
                            //放入trans监控
                            RudpMonitorUtil.callokTrans(transData.getStringTrans(), System.currentTimeMillis());

                        }
                    }
                } catch (Exception e) {
                    logger.error("调用计费服务错误错误" + reqObj.toString(), e);
                    respObj = reqObj;
                    respObj.put(RudpParamDefine.cmd_param_code, SendResultEnum.BILL_ERR_OUT_OF_SERVICE.value());

                }
            }
        } catch (Exception e) {
            logger.error(transData.transId + "业务数据组装错误", e);
        }

        

        

        try {
            if (respObj != null) {
            	respObj.put(RudpParamDefine.cmd_param_cmd_type, String.valueOf(RudpCmdDefine.cmd_type_ack));
                RudpDataUtil.sendDataAsyn(respObj, transData.remoteAddr, transData.getStringTrans());
            }
        } catch (Exception e) {
            logger.error(transData.transId + "发送反馈结果失败", e);
        }


        //if(RudpMonitorUtil.monitorFlag){
        RudpMonitorUtil.endBusiThread(System.currentTimeMillis() - startIme);
        //}

    }
}
